package cn.kgc.jinxin.clothesstore.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.kgc.jinxin.clothesstore.dao.BaseDao;
import cn.kgc.jinxin.clothesstore.dao.UserDao;
import cn.kgc.jinxin.clothesstore.entity.Category;
import cn.kgc.jinxin.clothesstore.entity.User;

public class UserDaoImpl implements UserDao{

	@Override
	public List<User> selectAll() throws Exception {
		List<User> userList = new ArrayList<User>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "select username,userbirthdate,userphone,Usergender from `user`";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			User user;
			while (resultSet.next()) {
				user = new User();
				user.setUserName(resultSet.getString(1));
				user.setUserBirthdate(resultSet.getDate(3));
				user.setUserPhone(resultSet.getNString(4));
				user.setUserGender(resultSet.getString(5).charAt(0));
				userList.add(user);
			} 
		} finally {
			BaseDao.close(connection, preparedStatement, resultSet);
		}
		return userList;
	}

	@Override
	public User selectById(String userName, String userPwd) throws Exception {
		User user = null;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "select * from `user` where username=? and userpwd=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, userName);
			preparedStatement.setString(1, userPwd);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				user = new User();
				user.setUserName(resultSet.getString(1));
				user.setUserPwd(resultSet.getString(2));
				user.setUserBirthdate(resultSet.getDate(3));
				user.setUserPhone(resultSet.getString(4));
				user.setUserGender(resultSet.getString(5).charAt(0));
			} 
		} finally {
			BaseDao.close(connection, preparedStatement, resultSet);
		}
		return user;
	}

	@Override
	public void insert(User user) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "insert into `user` VALUES (?,?,?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, user.getUserName());
			preparedStatement.setString(2, user.getUserPwd());
			preparedStatement.setDate(3, new Date(new java.util.Date().getTime()));
			preparedStatement.setString(4, user.getUserPhone());
			preparedStatement.setString(5, user.getUserGender()+"");
			preparedStatement.executeUpdate();
		} finally {
			BaseDao.close(connection, preparedStatement, null);
		}		
		
	}

	@Override
	public void update(User user, String userName, String userPwd) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "update `user` set username=? where username=? and userpwd=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, user.getUserName());
			preparedStatement.setString(2, userName);
			preparedStatement.setString(3, userPwd);
			preparedStatement.executeUpdate();
		} finally {
			BaseDao.close(connection, preparedStatement, null);
		}
		
	}

	@Override
	public void deleteByUserName(String userName, String userPwd) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "delete from `user` where userName=? and userPwd=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, userName);
			preparedStatement.setString(2, userPwd);
			preparedStatement.executeUpdate();
		} finally {
			BaseDao.close(connection, preparedStatement, null);
		}
		
	}

}
